package com.chen.bpsptsystem.service;

import com.chen.bpsptsystem.common.ServerResponse;
import com.chen.bpsptsystem.dao.MaterialMapper;
import com.chen.bpsptsystem.dao.MyMapper;
import com.chen.bpsptsystem.dao.UserMaterialMapper;
import com.chen.bpsptsystem.po.Material;
import com.chen.bpsptsystem.po.MaterialExample;
import com.chen.bpsptsystem.po.UserClazz;
import com.chen.bpsptsystem.po.UserMaterial;
import com.chen.bpsptsystem.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @author chen
 */
@Service
public class IMaterialService {

    @Resource
    private MaterialMapper materialMapper;

    @Resource
    private UserMaterialMapper userMaterialMapper;

    @Resource
    private MyMapper myMapper;

    public ServerResponse add(Material material) {

        int row = materialMapper.insertSelective(material);
        if (row > 0) {
            return ServerResponse.createBySuccess(material);
        }
        return ServerResponse.createByErrorMsg("添加失败");
    }

    public ServerResponse list(Integer pn, Integer pageSize, String clazzId) {
        PageHelper.startPage(pn, pageSize);
        MaterialExample example = new MaterialExample();
        MaterialExample.Criteria criteria = example.createCriteria();
        criteria.andClazzIdEqualTo(clazzId);
        List<Material> list = materialMapper.selectByExample(example);
        PageInfo<Material> info = new PageInfo<>(list);
        return ServerResponse.createBySuccess(info);
    }


    public ServerResponse edit(Material material) {
        Material one = materialMapper.selectByPrimaryKey(material.getId());
        if (null == one) {
            return ServerResponse.createByErrorMsg("资料不存在");
        }
        BeanUtils.copyProperties(material, one);
        int row = materialMapper.updateByPrimaryKeySelective(material);
        if (row > 0) {
            return ServerResponse.createBySuccess(one);
        }
        return ServerResponse.createByErrorMsg("修改失败");
    }

    public ServerResponse delete(Integer mId) {
        int row = materialMapper.deleteByPrimaryKey(mId);
        if (row > 0) {
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMsg("删除失败");
    }

    public ServerResponse distribution(Integer clazzId, String mIds) {
        List<Integer> mIdlist = StringUtil.splitToListInt(mIds);
        List<UserClazz> userClazzes = myMapper.selectByClazzId(clazzId);
        if (CollectionUtils.isNotEmpty(userClazzes)) {
            userClazzes.forEach(u -> {
                mIdlist.forEach(m -> {
                    UserMaterial userMaterial = myMapper.findByUserIdAndMid(u.getUserId(), m);
                    if (userMaterial == null) {
                        userMaterial = new UserMaterial();
                        userMaterial.setmId(m);
                        userMaterial.setUserId(u.getUserId());
                        userMaterial.setNum(0);
                        userMaterial.setCreateTime(new Date());
                        userMaterial.setUpdateTime(new Date());
                        userMaterial.setProgram(0.00);//进度为0
                        userMaterialMapper.insert(userMaterial);
                    }
                });
            });
            return ServerResponse.createBySuccess();
        } else {
            return ServerResponse.createByErrorMsg("分配失败");
        }
    }

    @Transactional
    public ServerResponse findById(Integer mId, Integer userId) {
        Map<String, Object> material = materialMapper.findById(mId);
        UserMaterial userMaterial = myMapper.selectByUserIdAndMId(userId, mId);
        if (userMaterial == null) {
            userMaterial = new UserMaterial();
            userMaterial.setmId(mId);
            userMaterial.setUserId(userId);
            userMaterial.setCreateTime(new Date());
            userMaterial.setUpdateTime(new Date());
            userMaterial.setProgram(0.00);
//            userMaterial.setNum();
            //学习次数加一
            userMaterial.setNum(1);
            userMaterialMapper.insert(userMaterial);
        } else {
            userMaterial.setmId(mId);
            userMaterial.setUserId(userId);
            //学习次数加一
            userMaterial.setNum(userMaterial.getNum() + 1);
            userMaterial.setUpdateTime(new Date());
            if (userMaterial.getProgram() >= 1) {
                userMaterial.setProgram(1.00);
            } else {
                userMaterial.setProgram(userMaterial.getProgram() + 0.2);
            }
            userMaterialMapper.updateByPrimaryKeySelective(userMaterial);
        }
        if (material == null) {
            return ServerResponse.createByErrorMsg("资料不存在");
        }
        material.put("program", userMaterial.getProgram());
        return ServerResponse.createBySuccess(material);
    }

    /**
     * 评价
     *
     * @param userId
     * @param mId
     * @param note
     * @return
     */
    @Transactional
    public ServerResponse evaluation(Integer userId, Integer mId, String note) {
        UserMaterial userMaterial = myMapper.selectByUserIdAndMId(userId, mId);
        if (userMaterial == null) {
            return ServerResponse.createByErrorMsg("资料信息不存在");
        }
        userMaterial.setNote(note);
        userMaterial.setmId(mId);
        userMaterial.setUserId(userId);
        userMaterial.setProgram(userMaterial.getProgram() + 0.2);
        //学习次数加一
        userMaterial.setNum(userMaterial.getNum() + 1);
        int row = userMaterialMapper.updateByPrimaryKeySelective(userMaterial);
        if (row > 0) {
            return ServerResponse.createBySuccessMsg("评价成功");
        }
        return ServerResponse.createByErrorMsg("评价失败");

    }

    public ServerResponse evaluationList(Integer mId, Integer pn, Integer pageSize) {
        PageHelper.startPage(pn, pageSize);
        List<Map<String, Object>> mapList = myMapper.evaluationList(mId);
        PageInfo pageInfo = new PageInfo(mapList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    /**
     * 我的兴趣班
     *
     * @param userId
     * @return
     */
    public ServerResponse myInteClass(Integer userId) {
        List<Map<String, Object>> mapList = null;
        Set<String> classIds = myMapper.selectClassId(userId);
        for (String c : classIds) {
            List<Integer> ids = StringUtil.splitToListInt(c);
            mapList = myMapper.myInteClass(ids);
        }
        return ServerResponse.createBySuccess(mapList);
    }
//
//    public ServerResponse detail(Integer id,Integer userId) {
//        Material detail = materialMapper.selectByPrimaryKey(id);
//        UserMaterial userMaterial = materialMapper.findHasRecord(id,userId);
//        return ServerResponse.createBySuccess(detail);
//    }
}
